﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1.segments.data_type
{
    class CM
    {
        public StringBuilder message_type { get; set; }
        public StringBuilder trigger_event { get; set; }
        public CM()
        {
            message_type = new StringBuilder();
            trigger_event = new StringBuilder();
        }

        enum segments { DG1, EVN, MSH, OBX, ORC, PID, PV1, RXO, NTE, OBR }

        string check(string source, int temp)
        {

            if (source[temp].ToString() + source[temp + 1].ToString() + source[temp + 2].ToString() == segments.MSH.ToString())
                return segments.MSH.ToString();
            else if (source[temp].ToString() + source[temp + 1].ToString() + source[temp + 2].ToString() == segments.DG1.ToString())
                return segments.DG1.ToString();
            else if (source[temp].ToString() + source[temp + 1].ToString() + source[temp + 2].ToString() == segments.EVN.ToString())
                return segments.EVN.ToString();
            else if (source[temp].ToString() + source[temp + 1].ToString() + source[temp + 2].ToString() == segments.NTE.ToString())
                return segments.NTE.ToString();
            else if (source[temp].ToString() + source[temp + 1].ToString() + source[temp + 2].ToString() == segments.OBR.ToString())
                return segments.OBR.ToString();
            else if (source[temp].ToString() + source[temp + 1].ToString() + source[temp + 2].ToString() == segments.OBX.ToString())
                return segments.OBX.ToString();
            else if (source[temp].ToString() + source[temp + 1].ToString() + source[temp + 2].ToString() == segments.ORC.ToString())
                return segments.ORC.ToString();
            else if (source[temp].ToString() + source[temp + 1].ToString() + source[temp + 2].ToString() == segments.PID.ToString())
                return segments.PID.ToString();
            else if (source[temp].ToString() + source[temp + 1].ToString() + source[temp + 2].ToString() == segments.PV1.ToString())
                return segments.PV1.ToString();
            else if (source[temp].ToString() + source[temp + 1].ToString() + source[temp + 2].ToString() == segments.RXO.ToString())
                return segments.RXO.ToString();

            return null;
        }

        public int fill(string source, int temp)
        {
            int x = 0, i;

            for (i = temp; i < source.Length; i++)
            {
                if (check(source, i) != null)
                {
                    message_type.Capacity = message_type.Length;
                    trigger_event.Capacity = trigger_event.Length;
                    return i - 1;
                }
                else if (source[i] == '^')
                    x++;
                else if (source[i] == '|')
                {
                    message_type.Capacity = message_type.Length;
                    trigger_event.Capacity = trigger_event.Length;
                    return i;
                }
                else if (x == 0)
                    message_type.Insert(message_type.Length, source[i]);
                else if (x == 1)
                    trigger_event.Insert(trigger_event.Length, source[i]);               
            }

            return i;
        }

        public string tostring()
        {
            string cm_string="";

            if (message_type != null)
                cm_string += message_type;
            if (trigger_event != null)
                cm_string += "^" + trigger_event;

            return cm_string;
        }
    }
}
